package cc.rengu.igas.channel.alipay.core.service.trans;

import cc.rengu.igas.channel.alipay.common.constant.AlipayConfigConstant;
import cc.rengu.igas.channel.alipay.common.constant.AlipayTreeNodeConstant;
import cc.rengu.igas.channel.alipay.common.enums.RespCodeEnum;
import cc.rengu.igas.channel.alipay.core.model.ChnlBizResponse;
import cc.rengu.igas.channel.alipay.core.service.base.UserService;
import cc.rengu.igas.channel.alipay.facade.request.AliGetAuthCodeRequest;
import cc.rengu.igas.channel.alipay.facade.response.AliGetAuthCodeResponse;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.entity.DstChannelCfg;
import cc.rengu.oltp.service.common.entity.SysParam;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.realize.SysParamService;
import cc.rengu.oltp.service.realize.impl.SysParamServiceImpl;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * 获取用户授权
 * 组装支付宝授权网页地址
 */
public class GetAuthCodeService extends UserService {
    @Override
    protected Object beforeBizProcess() throws Exception {
        AliGetAuthCodeRequest aliGetAuthCodeRequest = new AliGetAuthCodeRequest();
        ConvertUtil.convertOutput(aliGetAuthCodeRequest);
        return aliGetAuthCodeRequest;
    }

    @Override
    protected ChnlBizResponse callBizService(Object request) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        int iReturnCode = 0;
        AliGetAuthCodeResponse aliGetAuthCodeResponse = new AliGetAuthCodeResponse();
        AliGetAuthCodeRequest aliGetAuthCodeRequest = (AliGetAuthCodeRequest) request;
        DstChannelCfg dstChannelCfg = (DstChannelCfg) xmlTreeUtil.getXmlTreeObjectValue(AlipayTreeNodeConstant.DST_CHANNEL_CFG, DstChannelCfg.class);

        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.TXN_NUM, aliGetAuthCodeRequest.getHeader().getTxnNum());
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("app_id", dstChannelCfg.getAppId());
        paramMap.put("scope", "auth_base");
        paramMap.put("redirect_uri", URLEncoder.encode(aliGetAuthCodeRequest.getRedirectUrl(), "UTF-8"));
        paramMap.put("state", aliGetAuthCodeRequest.getState());
        SysParamService sysParamService = new SysParamServiceImpl();
        SysParam sysParam = sysParamService.getSysParamInfo(aliGetAuthCodeRequest.getHeader().getInstId(),
                AlipayConfigConstant.ALIPAY_CONF_NODE, AlipayConfigConstant.ALIPAY_AUTH_CODE_URL);
        if (sysParam == null) {
            throw new BizException(RespCodeEnum.CONFIG_PARAM_NOT_EXIST.getRespCode(), RespCodeEnum.CONFIG_PARAM_NOT_EXIST.getRespDesc());
        }
        String requestUrl = generateHttpUrl(sysParam.getParamValue(), paramMap);
        aliGetAuthCodeResponse.setAuthUrl(requestUrl);
        aliGetAuthCodeResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        aliGetAuthCodeResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        ChnlBizResponse chnlBizResponse = new ChnlBizResponse();
        chnlBizResponse.setResult(aliGetAuthCodeResponse);
        return chnlBizResponse;
    }

    @Override
    protected void afterBizProcess(ChnlBizResponse response) throws Exception {
        AliGetAuthCodeResponse aliGetAuthCodeResponse = (AliGetAuthCodeResponse) response.getResult();
        ConvertUtil.convertInput(aliGetAuthCodeResponse);
    }

    /**
     * 根据配置拼装URL地址
     *
     * @param transUrl    URL地址
     * @param urlParamMap URL参数集合
     * @return URL地址
     */
    private String generateHttpUrl(String transUrl, Map<String, String> urlParamMap) {
        String requestUrlParam = "";
        Iterator<String> it = urlParamMap.keySet().iterator();
        while (it.hasNext()) {
            String key = it.next();
            requestUrlParam += key + "=" + urlParamMap.get(key) + "&";
        }
        return transUrl + requestUrlParam.substring(0, requestUrlParam.length() - 1);
    }
}
